//
//  LogisticsViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "LogisticsViewController.h"
#import "PhoneLabel.h"
#import "LogisticsDetailModel.h"
#import "LogisticsListModel.h"
#import "LogisticsTableViewCell.h"

@interface LogisticsViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;
@property (weak, nonatomic) IBOutlet UILabel *orderStatusLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderExpressLabel;
@property (weak, nonatomic) IBOutlet UILabel *orderNumberLabel;
@property (weak, nonatomic) IBOutlet UIButton *servicePhoneButton;

@property (nonatomic, strong) LogisticsDetailModel *detailModel;
@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation LogisticsViewController
#pragma mark - Get
- (NSMutableArray *)tableArray {
    if (!_tableArray) {
        _tableArray = [[NSMutableArray alloc] init];
    }
    return _tableArray;
}
#pragma mark - IBAction
- (IBAction)serviceButtonAction:(id)sender {
    NSString *phone = self.servicePhoneButton.titleLabel.text;
    __block typeof(self) blockSelf = self;
    [self showAlertWithTitleArray:@[@"取消",@"呼叫"] title:nil message:phone alertStyle:UIAlertControllerStyleAlert];
    self.alertBlock = ^(NSString *actionTitle) {
        if ([actionTitle isEqualToString:@"呼叫"]) {
            NSString *telStr = [NSString stringWithFormat:@"tel:%@",phone];
            UIWebView * callWebview = [[UIWebView alloc] init];
            [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:telStr]]];
            [blockSelf.view addSubview:callWebview];
        }
    };
}
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"查看物流";
    [self createTableView];
    [self getNetData];
}
#pragma mark - action
static NSString *logisticsTableIdentifier = @"logisticsTableIdentifier";
- (void)createTableView {
    [self.tableView clearExtendCellLine];
    [self.tableView registerNib:[UINib nibWithNibName:@"LogisticsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:logisticsTableIdentifier];
}
- (void)updateHeaderView {
    [self.goodsImageView sd_setImageWithURL:GetUrl(self.detailModel.img) placeholderImage:PlaceholderImageSmall changeContentModel:UIViewContentModeScaleAspectFill];
    //MARK: 根据状态不同设置不同的颜色
    [self.orderStatusLabel setText:self.detailModel.status];
    
    [self.orderExpressLabel setText:[NSString stringWithFormat:@"%@",self.detailModel.express_company]];
    [self.orderNumberLabel setText:[NSString stringWithFormat:@"%@",self.detailModel.express_number]];
    
    [self.servicePhoneButton setTitle:[NSString stringWithFormat:@"%@",self.detailModel.service] forState:UIControlStateNormal];
}
#pragma mark - 网络请求
- (void)getNetData {
    //MARK:测试
    [self.orderStatusLabel setText:@"已完成"];
    [self.orderExpressLabel setText:@"韵达快递"];
    [self.orderNumberLabel setText:@"3907002020981"];
    [self.servicePhoneButton setTitle:@"95546" forState:UIControlStateNormal];
    
    NSDictionary *dic = @{@"content":@"在辽宁大连沙河口区西安路公司锦辉商城分部进行派件扫描；派送业务员：邓忠和；联系电话：15668603948",
                          @"createtime":@"2017-07-09 12:27:55"};
    for (int i = 0; i < 3; i++) {
        LogisticsListModel *model = [LogisticsListModel mj_objectWithKeyValues:dic];
        [self.tableArray addObject:model];
    }
    [self.tableView reloadData];
    return;
    
    //MARK:测试 获取物流详细信息
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:@"" parameters:@{@"orderid":self.orderID} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            self.detailModel = [LogisticsDetailModel mj_objectWithKeyValues:responseObject[@"data"]];
            [self updateHeaderView];
            [self getNetListData];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}

- (void)getNetListData {
    //MARK:测试 获取物流列表数据
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:@"" parameters:@{@"orderid":self.orderID} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            for (NSDictionary *dic in responseObject[@"data"]) {
                LogisticsListModel *model = [LogisticsListModel mj_objectWithKeyValues:dic];
                [self.tableArray addObject:model];
            }
            [self.tableView reloadData];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LogisticsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:logisticsTableIdentifier forIndexPath:indexPath];
    LogisticsListModel *model = self.tableArray[indexPath.row];
    [cell updateWithModel:model indexPath:indexPath];
    
    cell.contentLabel.PhoneClick = ^(NSString *phoneStr) {
        [self showAlertWithTitleArray:@[@"取消",@"呼叫"] title:nil message:cell.contentLabel.phoneStr alertStyle:UIAlertControllerStyleAlert];
        self.alertBlock = ^(NSString *actionTitle) {
            if ([actionTitle isEqualToString:@"呼叫"]) {
                [cell.contentLabel phone];
            }
        };
    };
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 72;
}

#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
//主提示标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    return [[NSAttributedString alloc] initWithString:@"暂无物流信息"
                                           attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0],
                                                        NSForegroundColorAttributeName:[UIColor grayTextColor]}];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getNetListData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
